from flask import Flask, session
from illumine import render, url_to # 参考Illumine
import os

this_path = os.abspath(".")

app = Flask(__name__)

files = os.walk(this_path)

def run(code):
    def echo(text):
        exec("result ="+text)

    exec(code.join(""))
    return result

def parse(path):
    for p in path:
        for i in p[2]:
            if p[0]+i == os.abspath(path):
                with open(path, 'r') as f:
                    lines = f.readlines()
                    count = 0
                    ep_start = 0
                    for line in lines:
                        count += 1
                        if line.find("#"):
                            lines[count] = line[line.find("#"):]
                        elif line.find("<?ep") != -1:
                            lines[count] -= line[line.find("<?ep"):]
                            ep_start = count
                        elif line.find("?>") != -1:
                            if ep_start != 0:
                                for j in range(ep_start+1, count-1):
                                    del lines[j]
                                lines.insert(run(lines[ep_start+1:count-1]),ep_start+1)
                            else:
                                print("Error,ep did not start,but use ?>")
    return lines.join("")


@app.route("\", method=["GET","POST"])
def myindex():
    return parse(".\index.ep")

@app.route("\<args>", method=["GET","POST"])
def web(args):
    return parse('.\'+args.split('|').join('\'))

if __name__ == "__main__":
    app.run(debug=True)